package com.w.wx.controller;


import com.w.wx.domain.Msg;
import com.w.wx.service.impls.RedisServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/wx/liked")
public class LikedContro {

    @Autowired
    private RedisServiceImpl redisService;

    /**
     * 保存点赞数据到redis
     * 点赞数+1
     * 同一个不能点赞两次
     * @param objId
     * @param openid
     * @param type
     * @return
     */
    @RequestMapping("like")
    public Msg saveLikedToRedis(Integer objId,String openid , String type){

        redisService.incrementLikedCount(objId, type, openid);

        redisService.saveLikedToRedis(objId,openid,type);

        int oneInfoCount = redisService.getOneInfoCount(objId, type);
        return Msg.success().add("count",oneInfoCount);
    }


    @RequestMapping("unlike")
    public  Msg decrementLikedCount(Integer objId,String openid,String type){
        redisService.decrementLikedCount(objId,type,openid);
        redisService.unlikeFromRedis(objId,openid,type);

        int oneInfoCount = redisService.getOneInfoCount(objId, type);
        return Msg.success().add("count",oneInfoCount);
    }

    @RequestMapping("restore")
    public Msg restoreRedisCountInfo(){
        redisService.savaInfoFromDb2Re(0);
        redisService.savaInfoFromDb2Re(1);
        redisService.savaInfoFromDb2Re(2);
        return Msg.success();
    }







}
